package com.example.mq1.mqserver.core;

import java.io.Serializable;
import java.util.UUID;

/**
 * 消息
 */
public class Message implements Serializable {

    //消息属性
    private BasicProperties basicProperties = new BasicProperties();
    //消息正文
    private byte[] body;

    //消息偏移量(使用 transient 修饰表示不被序列化，因为它不需要保存到硬盘上)
    private transient long offsetBeg;
    private transient long offsetEnd;

    //表示消息是否有效(消息采用文件删除)
    //0x1 表示有效，0x0 表示无效
    private byte isValid = 0x1;

    /**
     * 通过一个工厂化的方法来创建 message
     * 通过此方法生成的 message 对象有唯一的 messageId
     * @return
     */
    public static Message createMessageWithId(String routingKey, BasicProperties basicProperties, byte[] body) {
        Message message = new Message();
        message.setMessageId("M-" + UUID.randomUUID().toString());
        message.setRoutingKey(routingKey);
        if(basicProperties != null) {
            message.setBasicProperties(basicProperties);
        }
        message.setBody(body);
        return message;
    }

    public String getMessageId() {
        return basicProperties.getMessageId();
    }

    public void setMessageId(String messageId) {
        basicProperties.setMessageId(messageId);
    }

    public String getRoutingKey() {
        return basicProperties.getRoutingKey();
    }

    public void setRoutingKey(String routingKey) {
        basicProperties.setRoutingKey(routingKey);
    }

    public int getDeliverMode() {
        return basicProperties.getDeliverMode();
    }

    public void setDeliverMode(int deliverMode) {
        basicProperties.setDeliverMode(deliverMode);
    }

    public BasicProperties getBasicProperties() {
        return basicProperties;
    }

    public void setBasicProperties(BasicProperties basicProperties) {
        this.basicProperties = basicProperties;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public long getOffsetBeg() {
        return offsetBeg;
    }

    public void setOffsetBeg(long offsetBeg) {
        this.offsetBeg = offsetBeg;
    }

    public long getOffsetEnd() {
        return offsetEnd;
    }

    public void setOffsetEnd(long offsetEnd) {
        this.offsetEnd = offsetEnd;
    }

    public byte getIsValid() {
        return isValid;
    }

    public void setIsValid(byte isValid) {
        this.isValid = isValid;
    }
}
